/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gossipinator;

import net.sourceforge.jxa.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
 *
 * @author trilby
 */
public class ExoStorage implements IStorage, XmppListener {

    private Jxa jxa;
    private static final String id="wnathancox";
    private static final String passwd="coxboy61";
    private static final String server="talk.google.com";
    private static final String port="5223";
    private static int index=1;
    private static String message;

    public synchronized void onConnFailed(String msg) {
        System.out.println("confail "+msg+"\n");
        notifyAll();
    }

    public synchronized void onAuth(final String resource) {
        System.out.println("Auth "+resource+"\n");
        notifyAll();
    }

    public void onAuthFailed(final String message) {
        System.out.println("authfail "+message+"\n");
    }

    public synchronized void onMessageEvent(final String from, final String body) {
        System.out.println(from+" "+body+"\n");
        this.message=body;
        notifyAll();
    }

    public void onContactEvent(final String jid, final String name, final String group, final String subscription) {
        System.out.println("oCE "+jid+"\n");
    }

    public void onContactOverEvent() {}

    public synchronized void onStatusEvent(final String jid, final String show, final String status) {
        System.out.println("oSEv "+jid+"\n");
        notifyAll();
    }

    public void onSubscribeEvent(final String jid) {
        System.out.println("oSE "+jid+"\n");
    }

    public void onUnsubscribeEvent(final String jid) {
        System.out.println("oUE" +jid+"\n");
    }

    public synchronized boolean init(){
            jxa = new Jxa(id, passwd, "mobile", 10, server, port, true);
            jxa.addListener(this);
            jxa.start();
            
            try {
//              Thread.sleep(8000);            	
                wait();
            } catch (InterruptedException e) {}

            //jxa.sendMessage("commander@m2.exosite.com","setcik 5764a6ec7cdb380659c51c344c5af708e8438a98");
            return true;
        }
        public boolean deinit(){
            return true;
        }
        public boolean hasnext(){
            return true;
        }
        public synchronized ByteArrayInputStream read(){
            byte[] byteInputData = new byte[300];
            ByteArrayInputStream bs = new ByteArrayInputStream(byteInputData);
            jxa.sendMessage("commander@m2.exosite.com","setcik 5764a6ec7cdb380659c51c344c5af708e8438a98");
            try {
                wait();
            } catch (InterruptedException e) {}
            jxa.sendMessage("commander@m2.exosite.com","read "+index++);
            try { 
                wait();
            } catch (InterruptedException e) {}
            System.out.println("Got response!");

            //somehow wait for response before continuing...
            return bs;
        }
        public boolean write(ByteArrayOutputStream outputStream, int index){
            byte[] outputRecord;

            jxa.sendMessage("commander@m2.exosite.com","setcik 5764a6ec7cdb380659c51c344c5af708e8438a98");
            outputRecord = outputStream.toByteArray();
            jxa.sendMessage("commander@m2.exosite.com","write "+index+" "+outputRecord);
            return true;
        }
        public int count(){
            
	    jxa.sendMessage("commander@m2.exosite.com","read 1");
            return 0;
        }
}
